import datetime

from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAdminUser, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import GoodsVisitCount
from meiduo_admin.serialziers.statistical import UserGoodsCountSerializer
from users.models import User

#用户总量统计
class UserCountView(APIView):
    permission_classes=[IsAdminUser]
    def get(self,request):
        #1、获取当天日期
        now_data= datetime.date.today()
        # 2、获取用户总量
        count=User.objects.all().count()
        # 3、返回结果
        return Response({
            'data':now_data,
            'count':count
        })


#日增用户统计
class UserDayCountView(APIView):
    permission_classes = [IsAdminUser]
    def get(self,request):
        #1、获取当天日期
        now_data= datetime.date.today()
        # 2、获取日增用户总量
        count=User.objects.filter(date_joined__gte=now_data).count()
        # 3、返回结果
        return Response({
            'data':now_data,
            'count':count
        })

#日活用户统计
class UserDayActiveView(APIView):
    permission_classes = [IsAdminUser]
    def get(self,request):
        #1、获取当天日期
        now_data= datetime.date.today()
        # 2、获取日增用户总量
        count=User.objects.filter(last_login__gte=now_data).count()
        # 3、返回结果
        return Response({
            'data':now_data,
            'count':count
        })

#月增用户统计(从当天往前推30天,注册的用户)
class UserMonthCountView(APIView):
    permission_classes = [IsAdminUser]
    def get(self,request):
        #1、获取当天日期
        now_data= datetime.date.today()
        begin_data = now_data - datetime.timedelta(days=29)
        date_list=[]
        for i in range(30):
            # 2、起始日期
            index_data=begin_data+datetime.timedelta(days=i)
            # 3、第二天的日期
            next_data=begin_data+ datetime.timedelta(days=i+1)
            count=User.objects.filter(date_joined__gte=index_data,date_joined__lt=next_data).count()

            date_list.append({
                'data':index_data,
                'count':count
            })
        # 3、返回结果
        return Response (date_list)




#日分类商品访问量统计
class UserGoodsCountView(APIView):
    permission_classes = [IsAdminUser]
    def get(self,request):
        #1、获取当天日期
        now_data= datetime.date.today()
        # 2、获取当天分类访问数量
        goods=GoodsVisitCount.objects.filter(date__gte=now_data)
        # 3、序列化器
        ser=UserGoodsCountSerializer(goods,many=True)
        # 3、返回结果
        return Response (ser.data)
